/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EliminarCanal.java
 *
 * Created on 26/01/2012, 03:23:34 PM
 */
package GUI;

import Control.CanalJpaController;
import Control.exceptions.IllegalOrphanException;
import Control.exceptions.NonexistentEntityException;
import Entidad.Canal;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pepe
 */
public class EliminarCanal extends javax.swing.JPanel {

    /** Creates new form EliminarCanal */
    public EliminarCanal() {
        initComponents();
        controlCanal = new CanalJpaController();
    }
    
    boolean encontrado = false;
    Canal c;
    CanalJpaController controlCanal;

    /** This method is called from within the constructor to
     * initialize t= he form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        nacionalCB = new javax.swing.JCheckBox();
        internacionalCB = new javax.swing.JCheckBox();
        buscarCB = new javax.swing.JButton();
        eliminarCB = new javax.swing.JButton();
        cancelarCB = new javax.swing.JButton();
        statusJL = new javax.swing.JLabel();

        jLabel1.setText("Nombre");

        nacionalCB.setText("Nacional");
        nacionalCB.setEnabled(false);

        internacionalCB.setText("Internacional");
        internacionalCB.setEnabled(false);

        buscarCB.setText("Buscar");
        buscarCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarCBActionPerformed(evt);
            }
        });

        eliminarCB.setText("Eliminar");
        eliminarCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarCBActionPerformed(evt);
            }
        });

        cancelarCB.setText("Cancelar");
        cancelarCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarCBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(statusJL, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(buscarCB)
                                .addGap(18, 18, 18)
                                .addComponent(eliminarCB)
                                .addGap(18, 18, 18)
                                .addComponent(cancelarCB)
                                .addContainerGap())
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                                .addContainerGap())))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(nacionalCB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                        .addComponent(internacionalCB)
                        .addGap(81, 81, 81))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(internacionalCB)
                    .addComponent(nacionalCB))
                .addGap(19, 19, 19)
                .addComponent(statusJL, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelarCB)
                    .addComponent(eliminarCB)
                    .addComponent(buscarCB))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void buscarCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarCBActionPerformed
    c = controlCanal.buscarPorNombre(nombreTF.getText());
    statusJL.setText("");
    
    if(nombreTF.getText().equals("")){
        statusJL.setText("El nombre no puede estar vacio");
    }else{
        if(c == null){
            statusJL.setText("El canal no ha sido encontrado");
            nombreTF.setText("");
            encontrado = false;
        }else{
            nacionalCB.setSelected(c.getNacional());
            internacionalCB.setSelected(!c.getNacional()); 
            encontrado = true;
        }
    }
}//GEN-LAST:event_buscarCBActionPerformed

private void eliminarCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarCBActionPerformed
    if(encontrado){
        try {
            controlCanal.destroy(c.getIdCanal());
            statusJL.setText("El canal ha sido eliminado exitosamente");
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(EliminarCanal.class.getName()).log(Level.SEVERE, null, ex);
            statusJL.setText("No se ha podido eliminar el canal");
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(EliminarCanal.class.getName()).log(Level.SEVERE, null, ex);
            statusJL.setText("No se ha podido eliminar el canal");
        }
    }else{
        statusJL.setText("No hay canal a eliminar");
    }
}//GEN-LAST:event_eliminarCBActionPerformed

private void cancelarCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarCBActionPerformed
    nombreTF.setText("");
    nacionalCB.setSelected(false);
    internacionalCB.setSelected(false);
}//GEN-LAST:event_cancelarCBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscarCB;
    private javax.swing.JButton cancelarCB;
    private javax.swing.JButton eliminarCB;
    private javax.swing.JCheckBox internacionalCB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JCheckBox nacionalCB;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JLabel statusJL;
    // End of variables declaration//GEN-END:variables
}
